Ext.define('Admin.view.system.vote.VoteController', {

	extend : 'Admin.ux.GridController',

	alias : 'controller.vote',

	//选择记录
	onSelectionChange : function(grid, selection) {
		// var status = this.lookupReference('status');
		var viewModel = this.getView().getViewModel();
		var records = grid.getSelection();
		if (records.length == 1) {
			viewModel.set('giveAuthBtnStatus', false);
		} else {
			viewModel.set('giveAuthBtnStatus', true);
		}
	},
//=============================用户投票功能=================================================
	//投票
	voteAction:function(grid, rowIndex, colIndex,item,e,record,row){
		var rec = grid.getStore().getAt(rowIndex);
		var me = this;
		me.loadOption(me,rec.get('id'));
		var store= Ext.create('Ext.data.Store',{
       	 fields: ['boxLabel', 'name', 'inputValue'],
       	 proxy: {
		        type: 'ajax',
		        url: '/vote/optionjson',
		        reader: {
		            type: 'json'
		        }
		    },
		    autoLoad:false
       });
		
		var group= new Ext.form.RadioGroup({
	        xtype: 'radiogroup',
	        id: 'optionRadioGroup',
	        labelWidth: 80,
	        allowBlank:false,
	        labelAlign:'right',
	        fieldLabel: '选项',    
	        columns: 1,
	    });
		var voteform =  new Ext.FormPanel({
			  scrollable: false,
			  items:{xtype:'user-vote-form'},
			  
		});
		var groupform =  new Ext.FormPanel({
			  border:false,
			  items:[group]
		});
		
		store.load(function(records,operation,success){
			Ext.Array.each(records,function(model){
				var boxLabel = model.get('boxLabel');
				var name = model.get('name');
				var inputValue = model.get('inputValue');
				var radio=new Ext.form.field.Radio({ boxLabel: boxLabel, name: name, inputValue: inputValue });
				group.add(radio);
			})
		});

		var win = Ext.create("Ext.window.Window", {
			title : '投票',
			animateTarget : row,
			layout: {
		        type:'vbox',
		        align:'stretch'
		    },
			scrollable: true,
			
			width : '50%',
			height : '85%',
			items : [voteform,groupform],
			buttons : [
					{
						text : '投票',
						ui : 'soft-green',
						iconCls : 'x-fa 	fa-floppy-o',
						handler : function(btn) {
							var form = btn.up("window").down("form").getForm();
							//获取选中的radiogroup的值
							var option_id = Ext.getCmp('optionRadioGroup').getValue().option_id;
							Ext.create('Admin.ux.Action').submit(form,
									'/vote/userVote?option_id='+option_id, me.getView(), win);
						}
					}, 
					{
						text : '取消',
						ui : 'gray',
						iconCls : 'x-fa 	fa-ban',
						handler : function(btn) {
							btn.up("window").close();
						}
					}
			],
			listeners : {
				show : function(w, opts) {
					var form = this.down('form');
					form.getForm().load({
						url : '/vote/get',
						waitMsg : '加载中...',
						params : {
							id : rec.get('id')
						},
					});
				}
			}

		}).show();
	},
	//投票记录
	LogAction:function(b){
		var me = this;
		var vote_id = me.getView().getSelection()[0].get('id');
		me.loadOption(me,vote_id);
    	var win = Ext.create("Admin.ux.Window",{
    		title:'投票记录',
    		width:'45%',
    		height:'85%',
    		animateTarget: b,
    		items:{xtype:'votelog--view'},
    	});	
	},

	//选项列表
	showOptionAction:function(b){
		var me = this;
		var vote_id = me.getView().getSelection()[0].get('id');
		me.loadOption(me,vote_id);
    	var win = Ext.create("Admin.ux.Window",{
    		title:'投票选项',
    		width:'45%',
    		height:'85%',
    		animateTarget: b,
    		items:{xtype:'option-view'},
    	});	
	},
	//加载option
	loadOption:function(view,id){
    	//先把vote_id传参过去存在session
    	var store = view.getViewModel().getStore('data');	
    	Ext.apply(store.proxy.extraParams,{vote_id : id});
		store.load();
	},
	//生成选项
	autoAction : function(b) {
		var me = this;
		var vote_id = me.getView().getSelection()[0].get('id'); 
		
		var win = Ext.create("Admin.ux.Window", {
			title : '生成选项',
			animateTarget : b,
			width : '35%',
			height : '40%',
			items : { xtype: 'option-form'},  
			buttons : [
					{
						text : '保存',
						ui : 'soft-green',
						iconCls : 'x-fa 	fa-floppy-o',
						handler : function(btn) {
							var form = btn.up("window").down("form").getForm();
							Ext.create('Admin.ux.Action').submit(form,
									'/vote/autoOption?vote_id='+vote_id, me.getView(), win);
						}
					}, {
						text : '重置',
						ui : 'soft-red',
						iconCls : 'x-fa 	fa-mail-reply',
						handler : function(btn) {
							btn.up("window").down("form").reset();
						}
					}, {
						text : '取消',
						ui : 'gray',
						iconCls : 'x-fa 	fa-ban',
						handler : function(btn) {
							btn.up("window").close();
						}
					} ]
		});
	},

	//新增投票
	addAction : function(b) {
		var me = this;
		var win = Ext.create("Admin.ux.Window", {
			title : '创建投票',
			animateTarget : b,
			width : '55%',
			height : '88%',
			items : {
				xtype : 'vote-form'
			},
			buttons : [
					{
						text : '保存',
						ui : 'soft-green',
						iconCls : 'x-fa 	fa-floppy-o',
						handler : function(btn) {
							var form = btn.up("window").down("form").getForm();
							Ext.create('Admin.ux.Action').submit(form,
									'/vote/add', me.getView(), win);
						}
					}, {
						text : '重置',
						ui : 'soft-red',
						iconCls : 'x-fa 	fa-mail-reply',
						handler : function(btn) {
							btn.up("window").down("form").reset();
						}
					}, {
						text : '取消',
						ui : 'gray',
						iconCls : 'x-fa 	fa-ban',
						handler : function(btn) {
							btn.up("window").close();
						}
					} ]
		});
	},
	//编辑行
	editAction : function(grid, rowIndex, colIndex, item, e, record, row) {

		var rec = grid.getStore().getAt(rowIndex);

		var me = this;

		var win = Ext.create("Admin.ux.Window", {
			title : '编辑投票',
			animateTarget : row,
			width : 600,
			height : 500,
			items : {
				xtype : 'vote-form'
			},
			buttons : [
					{
						text : '保存',
						ui : 'soft-green',
						iconCls : 'x-fa 	fa-floppy-o',
						handler : function(btn) {
							var form = btn.up("window").down("form").getForm();
							Ext.create('Admin.ux.Action').submit(form,
									'/vote/edit', me.getView(), win);
						}
					}, {
						text : '重置',
						ui : 'soft-red',
						iconCls : 'x-fa 	fa-mail-reply',
						handler : function(btn) {
							var form = btn.up("window").down("form");
							form.loadRecord(rec);
						}
					}, {
						text : '取消',
						ui : 'gray',
						iconCls : 'x-fa 	fa-ban',
						handler : function(btn) {
							btn.up("window").close();
						}
					}

			],
			listeners : {
				show : function(w, opts) {
					var form = this.down('form');
					form.getForm().load({
						url : '/vote/get',
						waitMsg : '加载中...',
						params : {
							id : rec.get('id')
						},
					});
				}
			}
		});

	},
	//删除行
	removeAction : function(grid, rowIndex, colIndex, item, e, record, row) {
		var rec = grid.getStore().getAt(rowIndex);
		var _id = rec.data.id;
		Ext.create('Admin.ux.Action').remove({
			id : _id
		}, '/vote/delete', this.getView(), row);
	},
});
